package com.base.myproject.client.Connector;

import java.util.HashMap;

import pl.balon.gwt.diagrams.client.connection.RectilinearTwoEndedConnection;
import pl.balon.gwt.diagrams.client.connector.Connector;
import pl.balon.gwt.diagrams.client.connector.UIObjectConnector;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.BoundaryDropController;
import com.base.myproject.client.tools.Debug;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HTML;


public class ConnectorPanel extends AbsolutePanel implements AsyncCallback<ConnectorObjectContainer>{

	HashMap<ConnectorObject, ConnectorObject> hm = new HashMap<ConnectorObject,ConnectorObject>();
	public ConnectorPanel()
	{
		this.setSize("1640","1480");
		init();
	}
	
	void init()
	{
//		ConnectorObject co = new ConnectorObject();
//		co.setHtmlstr("<a><u>assssssssssssssssfd</u></a>");
//		co.setXpos(10);
//		co.setYpos(20);
//		
//		ConnectorObject co2 = new ConnectorObject();
//		co2.setHtmlstr("<a><u>asfd</u></a>");
//		co2.setXpos(100);
//		co2.setYpos(400);
//		
//		hm.put(co, co2);
		aa();
	}
	void aa()
	{
		
//		for(ConnectorObject co:hm.keySet())
//		{
//			HTML h1 = new HTML(co.getHtmlstr());
//			this.add(h1, co.getXpos(), co.getYpos());
//			
//			ConnectorObject co2 = hm.get(co);
//			HTML h2 =  new HTML(co2.getHtmlstr());
//			this.add(h2, co2.getXpos(), co2.getYpos());
//			
//			Connector c1 = UIObjectConnector.wrap(h1);
//		     Connector c2 = UIObjectConnector.wrap(h2);
//		     RectilinearTwoEndedConnection connection = new RectilinearTwoEndedConnection(c1, c2);
//		     connection.appendTo(this);
////			this.add(connection);
////			connection.update();
//		}
		
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getConnectorObject(this);
	}

	@Override
	public void onFailure(Throwable caught) {
		
		
	}

	@Override
	public void onSuccess(ConnectorObjectContainer result) {
		
//		for(ConnectorObject co:result.v)
//		{
//			HTML h1 = new HTML(co.getHtmlstr());
//			this.add(h1, co.getXpos(), co.getYpos());
//
//		}
		
		
		  PickupDragController dragController = new PickupDragController(this, true) {

              public BoundaryDropController newBoundaryDropController(AbsolutePanel boundaryPanel, boolean allowDropping) {
                      return new BoundaryDropController(boundaryPanel, allowDropping) {
                              public void onMove(DragContext context) {
                                      super.onMove(context);
                                      UIObjectConnector c = UIObjectConnector.getWrapper(context.draggable);
                                            if (c != null) {
                                                    c.update();
                                            }
                              }
                      };
              }
  };
		Debug.printf(result.hm.size());
		for(ConnectorObject co:result.hm.keySet())
			{
			HTML h1 = new HTML(co.getHtmlstr());
			this.add(h1, co.getXpos(), co.getYpos());
			
			ConnectorObject co2 = result.hm.get(co);
			HTML h2 =  new HTML(co2.getHtmlstr());
			this.add(h2, co2.getXpos(), co2.getYpos());
			
			Connector c1 = UIObjectConnector.wrap(h1);
		     Connector c2 = UIObjectConnector.wrap(h2);
		     RectilinearTwoEndedConnection connection = new RectilinearTwoEndedConnection(c1, c2);
		    
//			this.add(connection);
//			connection.update();
		     dragController.makeDraggable(h1);
		     dragController.makeDraggable(h2);
		     connection.appendTo(this);
		}
		Debug.printf(this.getElement().getInnerHTML());

	}
}
